package ext.mango.ab.po;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ext.mango.base.Messages;
import ext.mango.base.POBase;
import ext.mango.functions.Func;
import ext.mango.ab.eo.EOABMessage;
import ext.mango.ab.so.SOABMessage;

import mango.functions.FuncString;
import mango.orm.Dao;
import mango.orm.DaoFactory;
import mango.orm.DBSession;
import mango.orm.DBSessionFactory;
import mango.orm.entity.Entity;
import mango.service.ServiceManager;
import mango.web.Info;

public class Message extends POBase {
    private Log log = LogFactory.getLog(Message.class);

    public boolean qMessage() {
        StringBuilder sql = new StringBuilder(
                "select * from ABMessage where avliable='1' and operator = @operator "); //$NON-NLS-1$
        String sendNo = this.getRequestParameter("sendNo"); //$NON-NLS-1$
        String startdt = this.getRequestParameter("startdt"); //$NON-NLS-1$
        String enddt = this.getRequestParameter("enddt"); //$NON-NLS-1$
        String batchNo = this.getRequestParameter("batchNo"); //$NON-NLS-1$
        if (!FuncString.isBlank(sendNo)) {
            sql.append("and sender like @sender "); //$NON-NLS-1$
            sendNo = "%" + sendNo + "%"; //$NON-NLS-1$ //$NON-NLS-2$
        }
        if (!FuncString.isBlank(startdt)) {
            sql.append("and senddate>=@startdt "); //$NON-NLS-1$
        }
        if (!FuncString.isBlank(enddt)) {
            sql.append("and senddate<=@enddt "); //$NON-NLS-1$
        }
        if (!FuncString.isBlank(batchNo)) {
            sql.append("and batchNo=@batchNo "); //$NON-NLS-1$
        }
        sql.append("order by senddate asc"); //$NON-NLS-1$
        Dao dao = DaoFactory.c(sql.toString(), EOABMessage.class)
                .setStart(this.getRequestInt("start")).setLimit(this.getRequestInt("limit")) //$NON-NLS-1$ //$NON-NLS-2$
                .setParamByName("operator", this.getCurrentUser().getId()) //$NON-NLS-1$
                .setParamByName("sender", sendNo).setParamByName("startdt", startdt) //$NON-NLS-1$ //$NON-NLS-2$
                .setParamByName("enddt", enddt).setParamByName("batchno", batchNo); //$NON-NLS-1$ //$NON-NLS-2$
        int count = dao.getTotalCount();
        List<Entity> ret = dao.listQuery();
        System.out.println(Entity.toJSON(ret, "message", count)); //$NON-NLS-1$
        this.setResult(Entity.toJSON(ret, "message", count)); //$NON-NLS-1$
        return true;
    }

    public boolean dMessage() {
        DBSession session = DBSessionFactory.getCurrentSession();
        String sql = "update ABMessage set avliable = '0' where id = ?"; //$NON-NLS-1$
        this.log.debug("delete id : " + this.getRequestParameters("ID")); //$NON-NLS-1$ //$NON-NLS-2$
        for (String id : this.getRequestParameters("ID")) { //$NON-NLS-1$
            session.update(sql, id);
        }
        session.commit();
        return true;
    }

    public boolean importMsg() {
        SOABMessage ms = ServiceManager.getService(SOABMessage.class);
        String batchNo = Func.getSequenceNo("B"); //$NON-NLS-1$
        this.putExtra("BATCHNO", batchNo); //$NON-NLS-1$
        boolean ret = ms.importMsg(this);
        if (ret) {
//            this.println("{success : true, batchNo: '" + batchNo + "'}");
            this.addInfo(Info.SUCCESS, batchNo);
        }
        else {
//            this.println("{success : false, msg: '导入失败！'}");
            this.addInfo(Info.ERROR, Messages.getString(this.getLanguage(), "importfailed")); //$NON-NLS-1$
        }
        return ret;
    }
}
